// @ts-ignore
import axios from 'axios'
// @ts-ignore
import qs from 'qs'
// @ts-ignore
import url_path from './config'
import router from '../router'
// axios.defaults.withCredentials = true
axios.defaults.headers['Content-Type'] = 'application/json'
// const url_path = '/api'
axios.interceptors.request.use((config) => {
    // let token = localStorage.getItem('token')
    // if(token){
    //     axios.defaults.headers['token'] = `${token}`
    // }
    // 获取token
    // if (location.pathname !== '/packinghouse') {
    //     if(window.localStorage.token){
    //         config.headers.token = window.localStorage.token
    //     }
    // }
    // 在请求头中携带token
    return config
})
const judgeLoginUser = (response:any) => {
    //配置异常code所作的处理开始
    if (response.data.code === 401) {
        //code4010是只token失效的时候发出的code4010，登录失败的code不应该是4010，所以需要让他把抛出的
        //code不应该是4010
        console.log(response.data)
        // router.push('/packinghouse')
        return
    }else if(response.data.code === 200){

    }else {

    }
    //配置异常code所作的处理结束
}

export default {
    get (url:any, params = {}) {
        return new Promise((resolve, reject) => {
            axios.get(url_path + url, {
                params
            })
                .then(response => {
                    judgeLoginUser(response)
                    resolve(response.data)
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
    post (url:any, params = {}) {
        return new Promise((resolve, reject) => {
            axios.post(url_path + url, params)
                .then(response => {
                    judgeLoginUser(response)
                    resolve(response.data)
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
    put (url:any, params = {}) {
        return new Promise((resolve, reject) => {
            axios.put(url_path + url, qs.stringify(params))
                .then(response => {
                    judgeLoginUser(response)
                    resolve(response.data)
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
    delete (url:any, params = {}) {
        return new Promise((resolve, reject) => {
            axios.delete(url_path+ url, { params })
                .then(response => {
                    judgeLoginUser(response)
                    resolve(response.data)
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
    pest (url:any, params = {}) {
        // headers={'Content-Type':'application/json;charset=UTF-8'},
        return new Promise((resolve, reject) => {
            axios.post(url_path + url+'?'+qs.stringify(params))
                .then(response => {
                    judgeLoginUser(response)
                    resolve(response.data)
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
    pest2 (url:any, data = {}) {
        // headers={'Content-Type':'application/json;charset=UTF-8'},
        return new Promise((resolve, reject) => {
            axios.post(url_path + url,data,{headers: { //添加请求头
                    "Content-Type": "multipart/form-data"
                }})
                .then(response => {
                    judgeLoginUser(response)
                    resolve(response.data)
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
    pestTest (url:any, params = {}) {
        return new Promise((resolve, reject) => {
            axios.post(url_path + url+'?'+qs.stringify(params), {},{headers: { //添加请求头
                }})
                .then(response => {
                    judgeLoginUser(response)
                    resolve(response.data)
                })
                .catch(err => {
                    reject(err)
                })
        })
    }
}
